/*
 * REFThreadEvent.h
 *
 *  Created on: Apr 26, 2013
 *      Author: Lucifer
 */

#ifndef THREADEVENT_H_
#define THREADEVENT_H_

namespace BFX
{

// Represents a thread event to notifies one or more waiting threads that an event has occurred.
class REFThreadEvent : public REFObject
{
public:
	/**
	 * Initializes a new instance of the BFX::REFThreadEvent class.
	 * @param[in] bInitialState True to set the initial state to signaled; false to set the initial state to non-signaled.
	 * @param[in] bAutoReset True to set notifies one of wating threads; false to set notifies all waiting threads.
	 */
	REFThreadEvent(bool bInitialState, bool bAutoReset = false);
	virtual ~REFThreadEvent() {}

	/**
	 * Blocks the current thread until the current event object receives a signal.
	 * @param[in] nMillisecondsTimeout The number of milliseconds to wait, or (-1) to wait indefinitely.
	 * @return True if the current instance receives a signal; otherwise, false.
	 * @see BFX::ThreadInterruptException, BFX::ThreadAbortException
	 */
	virtual bool Wait(int nMillisecondsTimeout = -1);
	/**
	 * Sets the state of the event to signaled, allowing one or more waiting threads to proceed.
	 */
	virtual void Set();
	/**
	 * Sets the state of the event to non-signaled, causing threads to block.
	 */
	virtual void Reset();

protected:

	volatile long	m_lState;
	bool	m_bAutoReset;
	SyncBlock	m_syncBlock;
};

}	//	namespace BFX

#endif /* THREADEVENT_H_ */
